#include <soar.h>

void transfer()
{/*{{{*/
#define BIN_SIZE        33
    unsigned char bin[BIN_SIZE];
    char str[BIN_SIZE*2+1];
    int i;

    unsigned int seed=0;
    for(i=0; i<BIN_SIZE; ++i)
        bin[i]=(unsigned char)random_int(0, 255, &seed);
    //bin[0]=0x23;  bin[1]=0xF1;  bin[2]=0xA9;  bin[3]=0x80;
    expand_to_str(bin, BIN_SIZE, str);
    printf("expand: %s\n", str);
//    for(i=0; i<BIN_SIZE*2; i++)
//        printf("%c", str[i]);
//    printf("\n");

    collapse_from_str(str, BIN_SIZE*2, bin);
    printf("collpase: ");
    for(i=0; i<BIN_SIZE; i++)
        printf("%02X", bin[i]);

    printf("\n=================================\n");

    return;
}/*}}}*/

void space()
{/*{{{*/
    char str[100]="   xxx  yyy   zzz   a b c cc ccc cccc    ";

    trim_head_space(str);
    printf("trim_head_space: |%s|\n", str);
    trim_tail_space(str);
    printf("trim_tail_space: |%s|\n", str);
    trim_redundant_space(str);
    printf("trim_redundant_space: |%s|\n", str);
    pad_space(str, 60);
    printf("pad_space: |%s|\n", str);

    printf("=================================\n");

    return;
}/*}}}*/

void wrap()
{/*{{{*/
    unsigned char msg[BUFFER_SIZE];
    int msg_size;

    strcpy((char *)msg, "Hello world!");
    msg_size=strlen((char *)msg);

    wrap_size(msg, &msg_size, msg);
    msg[msg_size]=ZERO;
    printf("msg wrap: %s\n", msg);
    printf("msg wrap size: %d\n", msg_size);

    unwrap_size(msg, &msg_size, msg);
    msg[msg_size]=ZERO;
    printf("msg: %s\n", msg);
    printf("msg size: %d\n", msg_size);

    printf("=================================\n");

    return;
}/*}}}*/

void split()
{/*{{{*/
    char strs[][100]=
    {
        "   xxx  yyy   zzz   a b c cc ccc cccc    ",
        "\ta\t\tb\tc\t",
        "    \tx   y\tz\t",
        "111|222|333|444|555",
        "aaa"
    };
    char **array;
    int i;
    int row_count;

    printf("str1（由空格分隔）：\"%s\"\n", strs[0]);
    printf("使用SPACE调用str_split，结果为：\n");
    row_count=str_split(strs[0], SPACE, &array); 
    for(i=0; i<row_count; i++)
        printf("%s\n", array[i]);
    FREE_PTR_ARRAY(array, row_count);
    printf("---------------------------\n");

    printf("str2（由\\t分隔）：\"%s\"\n", strs[1]);
    printf("使用SPACE调用str_split，结果为：\n");
    row_count=str_split(strs[1], SPACE, &array); 
    for(i=0; i<row_count; i++)
        printf("%s\n", array[i]);
    FREE_PTR_ARRAY(array, row_count);
    printf("使用TAB调用str_split，结果为：\n");
    row_count=str_split(strs[1], TAB, &array); 
    for(i=0; i<row_count; i++)
        printf("%s\n", array[i]);
    FREE_PTR_ARRAY(array, row_count);
    printf("---------------------------\n");

    printf("str3（由\\t和空格混合分隔）：\"%s\"\n", strs[2]);
    printf("使用SPACE调用str_split，结果为：\n");
    row_count=str_split(strs[2], SPACE, &array); 
    for(i=0; i<row_count; i++)
        printf("%s\n", array[i]);
    FREE_PTR_ARRAY(array, row_count);
    printf("使用TAB调用str_split，结果为：\n");
    row_count=str_split(strs[2], TAB, &array); 
    for(i=0; i<row_count; i++)
        printf("%s\n", array[i]);
    FREE_PTR_ARRAY(array, row_count);
    printf("使用0（兼顾空格和\\t）调用str_split，结果为：\n");
    row_count=str_split(strs[2], 0, &array); 
    for(i=0; i<row_count; i++)
        printf("%s\n", array[i]);
    FREE_PTR_ARRAY(array, row_count);
    printf("调用expand_tab将\\t转化为空格，结果为：");
    expand_tab(strs[2], 0, 0);
    printf("\"%s\"\n", strs[2]);
    printf("使用SPACE调用str_split，结果为：\n");
    row_count=str_split(strs[2], SPACE, &array); 
    for(i=0; i<row_count; i++)
        printf("%s\n", array[i]);
    FREE_PTR_ARRAY(array, row_count);
    printf("---------------------------\n");

    printf("str4（由|分隔）：\"%s\"\n", strs[3]);
    printf("使用|调用str_split，结果为：\n");
    row_count=str_split(strs[3], '|', &array); 
    for(i=0; i<row_count; i++)
        printf("%s\n", array[i]);
    FREE_PTR_ARRAY(array, row_count);
    printf("---------------------------\n");

    printf("str5（无分隔）：\"%s\"\n", strs[4]);
    printf("使用SPACE调用str_split，结果为：\n");
    row_count=str_split(strs[4], SPACE, &array); 
    for(i=0; i<row_count; i++)
        printf("%s\n", array[i]);
    FREE_PTR_ARRAY(array, row_count);
    printf("---------------------------\n");

    printf("=================================\n");
}/*}}}*/

void upper_lower()
{/*{{{*/
    char str[50];

    strcpy(str, "aaaBBBccc111");
    printf("str: %s\n", str);
    str_toupper(str);
    printf("str toupper: %s\n", str);

    strcpy(str, "aaaBBBccc111");
    str_tolower(str);
    printf("str tolower: %s\n", str);

    printf("=================================\n");
}/*}}}*/

void replace()
{/*{{{*/
    char str[100];
    char *str2;

    strcpy(str, "111xxx222xxx333");
    printf("替换'xxx'为'yyy': %s --> ", str);
    printf("%s\n", str_replace(str, "xxx", "yyy", str));

    strcpy(str, "1|2|3|");
    printf("替换'|'为',': %s --> ", str);
    printf("%s\n", str2=str_replace(str, "|", ",", NULL));
    free(str2);

    strcpy(str, "1|2|3|");
    printf("替换'|'为'||': %s --> ", str);
    printf("%s\n", str_replace(str, "|", "||", str));

    /*
    strcpy(str, "1\n2\n3\n");
    printf("替换'\\n'为'\\r\\n': %s --> ", str);
    printf("%s\n", replace_str(str, "\n", "\r\n", str));
    */

    printf("=================================\n");
}/*}}}*/

void insert()
{/*{{{*/
    char str[100];

    strcpy(str, "01234567");
    printf("原字符串: %s\n", str);
    printf("第1位前插入'a': %s\n", str_insert(str, 1, "a"));
    printf("倒数第1位前插入'b': %s\n", str_insert(str, -1, "b"));
    printf("首插入'xxx': %s\n", str_insert(str, 0, "xxx"));
    printf("尾插入'yyy': %s\n", str_insert(str, strlen(str), "yyy"));

    printf("=================================\n");
}/*}}}*/

void extract()
{/*{{{*/
    char filename[LONG_FILE_NM_SIZE+1];
    char name[LONG_FILE_NM_SIZE+1];

    printf("提取目录名\n");
    strcpy(filename, "/");
    printf("%s --> %s\n", filename, extract_dirname(filename, name));
    strcpy(filename, "/root");
    printf("%s --> %s\n", filename, extract_dirname(filename, name));
    strcpy(filename, "/opt/libcommon");
    printf("%s --> %s\n", filename, extract_dirname(filename, name));
    strcpy(filename, "/opt/libcommon/");
    printf("%s --> %s\n", filename, extract_dirname(filename, name));
    strcpy(filename, "libcommon");
    printf("%s --> %s\n", filename, extract_dirname(filename, name));
    strcpy(filename, "libcommon/");
    printf("%s --> %s\n", filename, extract_dirname(filename, name));
    strcpy(filename, ".");
    printf("%s --> %s\n", filename, extract_dirname(filename, name));
    strcpy(filename, "./");
    printf("%s --> %s\n", filename, extract_dirname(filename, name));
    strcpy(filename, "..");
    printf("%s --> %s\n", filename, extract_dirname(filename, name));
    strcpy(filename, "../");
    printf("%s --> %s\n", filename, extract_dirname(filename, name));

    printf("提取底层名\n");
    strcpy(filename, "/");
    printf("%s --> %s\n", filename, extract_basename(filename, name));
    strcpy(filename, "/root");
    printf("%s --> %s\n", filename, extract_basename(filename, name));
    strcpy(filename, "/opt/libcommon");
    printf("%s --> %s\n", filename, extract_basename(filename, name));
    strcpy(filename, "/opt/libcommon/");
    printf("%s --> %s\n", filename, extract_basename(filename, name));
    strcpy(filename, "libcommon");
    printf("%s --> %s\n", filename, extract_basename(filename, name));
    strcpy(filename, "libcommon/");
    printf("%s --> %s\n", filename, extract_basename(filename, name));
    strcpy(filename, ".");
    printf("%s --> %s\n", filename, extract_basename(filename, name));
    strcpy(filename, "./");
    printf("%s --> %s\n", filename, extract_basename(filename, name));
    strcpy(filename, "..");
    printf("%s --> %s\n", filename, extract_basename(filename, name));
    strcpy(filename, "../");
    printf("%s --> %s\n", filename, extract_basename(filename, name));

    printf("=================================\n");
}/*}}}*/

int main()
{/*{{{*/
    transfer();

    space();

    wrap();

    split();

    upper_lower(); 

    replace();

    insert();

    extract();

    return(0);
}/*}}}*/
